//
//  ViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/24.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    var data: [CellModel]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "主页"
        tableView = UITableView(frame: self.view.bounds, style: .grouped)
        self.view.addSubview(tableView)
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "UITableViewCell")
        tableView.separatorStyle = .none
        
        initData()
    }
    
    func initData(){
        data = [CellModel]()
        addCellModel(title: "create", vc: CreateViewController.classForCoder())
        
        addCellModel(title: "keyboard", vc: KeyboardViewController.classForCoder())
        addCellModel(title: "any gesture", vc: AnyGestureViewController.classForCoder())
        addCellModel(title: "Screen edge", vc: ScreenEdgeViewController.classForCoder())
        addCellModel(title: "transform gesture", vc: TransformViewController.classForCoder())
        addCellModel(title: "pinch gesture", vc: PinchGestureViewController.classForCoder())
        addCellModel(title: "rotation gesture", vc: RotationGestureViewController.classForCoder())
        addCellModel(title: "pan gesture", vc: PanGestureViewController.classForCoder())
        addCellModel(title: "long press gesture", vc: LongPressGestureViewController.classForCoder())
        addCellModel(title: "swip horizontal", vc: SwipHorizontalViewController.classForCoder())
        addCellModel(title: "swip down", vc: SwipDownGestureViewController.classForCoder())
        addCellModel(title: "double tap gesture", vc: DoubleTapGestureViewController.classForCoder())
        addCellModel(title: "tap gesture", vc: TapGestureViewController.classForCoder())
        addCellModel(title: "alert action", vc: AlertActionViewController.classForCoder())
        addCellModel(title: "action1", vc: Action1ViewController.classForCoder())
        addCellModel(title: "action", vc: ActionViewController.classForCoder())
        addCellModel(title: "双向绑定", vc: TwoWayBindingViewController.classForCoder())
        addCellModel(title: "moya", vc: MoyaViewController.classForCoder())
        addCellModel(title: "group table view", vc: GroupTableViewController.classForCoder())
        addCellModel(title: "tableView", vc: TableViewController.classForCoder())
        addCellModel(title: "driver", vc: DriverViewController.classForCoder())
        addCellModel(title: "register", vc: RegisterViewController.classForCoder())
        addCellModel(title: "calculator", vc: CalculatorViewController.classForCoder())
        addCellModel(title: "uitextfield", vc: UITextFieldViewController.classForCoder())
        addCellModel(title: "retry", vc: RetryViewController.classForCoder())
        addCellModel(title: "catch error", vc: CatchErrorViewController.classForCoder())
        addCellModel(title: "catchErrorJustReturn", vc: CatchErrorJustReturnViewController.classForCoder())
        addCellModel(title: "multicast", vc: MulticastViewController.classForCoder())
        addCellModel(title: "replay", vc: ReplayViewController.classForCoder())
        addCellModel(title: "publish", vc: PublishViewController.classForCoder())
        addCellModel(title: "concat", vc: ConcatViewController.classForCoder())
        addCellModel(title: "reduce", vc: ReduceViewController.classForCoder())
        addCellModel(title: "skip until", vc: SkipUntilViewController.classForCoder())
        addCellModel(title: "skip while with index", vc: SkipWhileWithIndexViewController.classForCoder())
        addCellModel(title: "skip while", vc: SkipWhileViewController.classForCoder())
        addCellModel(title: "take until", vc: TakeUntilViewController.classForCoder())
        addCellModel(title: "take while", vc: TakeWhileViewController.classForCoder())
        addCellModel(title: "single", vc: SingleViewController.classForCoder())
        addCellModel(title: "element at", vc: ElementAtViewController.classForCoder())
        addCellModel(title: "distinct until changed", vc: DistinctUntilChangedViewController.classForCoder())
        addCellModel(title: "filter", vc: FilterViewController.classForCoder())
        addCellModel(title: "scan", vc: ScanViewController.classForCoder())
        addCellModel(title: "flat map latest", vc: FlatmapLatestViewController.classForCoder())
        addCellModel(title: "flat map", vc: FlatmapViewController.classForCoder())
        addCellModel(title: "map", vc: MapViewController.classForCoder())
        addCellModel(title: "target", vc: TargetViewController.classForCoder())
        addCellModel(title: "switch latest", vc: SwitchLatestViewController.classForCoder())
        addCellModel(title: "combine latest", vc: CombineLatestViewController.classForCoder())
        addCellModel(title: "zip", vc: ZipViewController.classForCoder())
        addCellModel(title: "merge", vc: MergeViewController.classForCoder())
        addCellModel(title: "start with", vc: StartWithViewController.classForCoder())
        addCellModel(title: "variable", vc: VariableViewController.classForCoder())
        addCellModel(title: "BehaviorSubject", vc: BehaviorSubjectViewController.classForCoder())
        addCellModel(title: "ReplaySubject", vc: ReplaySubjectViewController.classForCoder())
        addCellModel(title: "publish subject", vc: PublishSubjectViewController.classForCoder())
        addCellModel(title: "doOn", vc: DoOnViewController.classForCoder())
        addCellModel(title: "error", vc: ErrorViewController.classForCoder())
        addCellModel(title: "deferred", vc: DeferredViewController.classForCoder())
        addCellModel(title: "generate", vc: GenerateViewController.classForCoder())
        addCellModel(title: "repeatElement", vc: RepeatElementViewController.classForCoder())
        addCellModel(title: "range", vc: RangeViewController.classForCoder())
        addCellModel(title: "from", vc: FromViewController.classForCoder())
        addCellModel(title: "subscribe", vc: SubscribeViewController.classForCoder())
        addCellModel(title: "of", vc: OfViewController.classForCoder())
        addCellModel(title: "just", vc: JustViewController.classForCoder())
        addCellModel(title: "empty", vc: EmptyViewController.classForCoder())
        addCellModel(title: "never", vc: NeverViewController.classForCoder())
        
    }
    
    func addCellModel(title: String, vc: AnyClass){
        weak var wself = self
        
        let cellModel = CellModel(wself, title, "\(vc)")
        data.append(cellModel)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        cell.textLabel?.text = data[indexPath.section].title
    
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        data[indexPath.section].action()
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.00001
    }
      
}

class CellModel {
    var title = ""
    var action: (() -> Void)!
    
    convenience init(_ vc: UIViewController?, _ title: String, _ controller: String) {
        self.init()
        self.title = title
        
        self.action = {
            [weak self] in
            
            let vcon = self?.stringClassObjectFromString(className: controller)
            vcon?.title = self?.title
            
            if let vcon = vcon {
                vc?.navigationController?.pushViewController(vcon, animated: true)
            }
            
        }
    }
    
    func stringClassObjectFromString(className: String) -> UIViewController! {
        
        /// 获取命名空间
        let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String;
        
        /// 根据命名空间传来的字符串先转换成anyClass
        let cls: AnyClass = NSClassFromString(namespace + "." + className)!;
        
        // 在这里已经可以return了   返回类型:AnyClass!
        //return cls;
        
        /// 转换成 明确的类
        let vcClass = cls as! UIViewController.Type;
        
        /// 返回这个类的对象
        return vcClass.init();
    }
}

func delay(_ time: Int, work: @escaping() -> Swift.Void){
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(time), execute: work)
}

